//
//  MyteamViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/9/27.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import Eureka
import SnapKit
import SwiftyUserDefaults

class MyteamViewController: BaseViewController ,UITableViewDataSource,UITableViewDelegate
{
    var titleArr0 = [String]()
    var titleArr1 = [String]()
    var teamType : NSString?
    var myDataModel = productModel();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeBaseUI()
        layoutNav()
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        layoutNav()
        getData()
    }
    func getData() {
        MyNetwork.request(target: .getTwoWayNdetail(user_id: Defaults[.userID]!, type: self.teamType! as String), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            
            if needModel.code == 1111{
                self.myDataModel.yy_modelSet(withJSON:(result as AnyObject)["data"]);
                self.myTableView.reloadData()
//                self.navigationController?.pushViewController(ConfirmZhuCeViewController(), animated: true)
                
            }else{
                self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
            }
            
        }) { (error) in
            //kLog(error.localizedDescription)
            self.navigationController?.view.makeToast(error.localizedDescription)
        }
    }
    //MARK: - Private methods
    func makeBaseUI(){
        self.view.addSubview(myTableView)
        
        titleArr0 = [XALanguageManager.getString(key: "Released"),XALanguageManager.getString(key: "Is releasing"),XALanguageManager.getString(key: "Not released")]
        titleArr1 = [XALanguageManager.getString(key: "关于我们"),XALanguageManager.getString(key: "联系我们")]
    }
    

    @objc func changeLanguage(){
        if (UserDefaults.standard.value(forKey: "langeuage") as! String) == "en"{ // 英语
            tabBarController?.title = XALanguageManager.getString(key: "设置")
        } else {
            
        }
    }
    func layoutNav(){
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        
        self.title = String(format: "%@ %@", XALanguageManager.getString(key: "My team"),self.teamType!) 
        
        self.navigationController?.isNavigationBarHidden = false
    }
    
    
    //MARK: - tableView Delegate And DataSource
    
    
    override func willMove(toParentViewController parent: UIViewController?) {
        
    }
    
    
    //MARK: - tableView Delegate And DataSource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let reuseIdentifier = "staticCellReuseIdentifier - \(indexPath.description)"
        
        var cell:MyTeamTableViewCell! = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)as?MyTeamTableViewCell
        if cell == nil {
            cell = MyTeamTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier)
        }
        cell.accessoryType = .disclosureIndicator
        if indexPath.section == 1 {
            cell?.titleLab?.text = titleArr0[indexPath.section - 1]
            if myDataModel.released != nil{
                cell.priceLab?.text = String(format: "%@ MT", myDataModel.released!)
            }
            
            cell.detailLable?.text  = ""
        }else if indexPath.section == 2{
            cell.titleLab?.text = titleArr0[indexPath.section - 1]
            if myDataModel.is_releasing != nil{
                cell.priceLab?.text = String(format: "%@ MT", myDataModel.is_releasing!)
            }
            
            
            cell.detailLable?.text  = ""
        }else if indexPath.section == 3 {
            cell.titleLab?.text = titleArr0[indexPath.section - 1]
            cell.detailLable?.text  = XALanguageManager.getString(key: "未达到释放条件")
            if myDataModel.not_released != nil{
                cell.priceLab?.text = String(format: "%@ MT", myDataModel.not_released!)
            }
            
            
            cell.contentView.layer.cornerRadius = 20
            
            cell.accessoryType = .none
        }else if indexPath.section == 0 {
            cell.accessoryType = .none
            
            let topphoto = UIImageView()
            topphoto.layer.cornerRadius = 23;
            cell.contentView.addSubview(topphoto)
            topphoto.snp.makeConstraints { (make) in
                make.centerX.equalTo(cell)
                make.size.equalTo(CGSize(width: 46, height: 46))
                make.top.equalTo(26)
            }
            
            topphoto.backgroundColor = UIColor("#346FF4")
            topphoto.sd_setImage(with: NSURL.init(string: "")! as URL, placeholderImage: UIImage.init(named: ""), options: [], progress: nil, completed:nil)
            
            
            let nameLaebl = UILabel()
            nameLaebl.textAlignment = .center
            cell.contentView.addSubview(nameLaebl);
            nameLaebl.snp.makeConstraints { (make) in
                make.top.equalTo(topphoto.snp.bottom).offset(25)
                make.size.equalTo(CGSize(width: 200, height: 16))
                make.centerX.equalTo(cell.contentView)
            }
            nameLaebl.textAlignment = .center
            nameLaebl.font = UIFont.boldSystemFont(ofSize:18)
            if myDataModel.total_releasing != nil{
                nameLaebl.text = String(format: "%@ MT", myDataModel.total_releasing!)
            }
            
            nameLaebl.textColor = UIColor("#346FF4")
            
            let leftLabel = UILabel()
            leftLabel.textAlignment = .center
            cell.contentView.addSubview(leftLabel);
            leftLabel.snp.makeConstraints { (make) in
                make.top.equalTo(nameLaebl.snp.bottom).offset(25)
                make.size.equalTo(CGSize(width: kScreenWidth - 100, height: 16))
                make.centerX.equalTo(cell.contentView)
            }
            leftLabel.textColor = UIColor("909090")
            leftLabel.textAlignment = .left
            leftLabel.font = UIFont.systemFont(ofSize: 15)
            if myDataModel.released != nil{
                leftLabel.text = String(format: "%@ %@", myDataModel.released!,XALanguageManager.getString(key: "Released"))
            }
            
            
            let rightLabel = UILabel()
            rightLabel.textAlignment = .right
            cell.contentView.addSubview(rightLabel);
            rightLabel.snp.makeConstraints { (make) in
                make.top.equalTo(nameLaebl.snp.bottom).offset(25)
                make.size.equalTo(CGSize(width: kScreenWidth - 100, height: 16))
                make.centerX.equalTo(cell.contentView)
            }
            rightLabel.textColor = UIColor("909090")
            rightLabel.textAlignment = .right
            rightLabel.font = UIFont.systemFont(ofSize: 15)
            if myDataModel.is_releasing != nil{
                rightLabel.text = String(format: "%@ %@", myDataModel.is_releasing!,XALanguageManager.getString(key: "Is releasing"))
            }
            
            let bottomLabel = UILabel()
            bottomLabel.textAlignment = .center
            cell.contentView.addSubview(bottomLabel);
            bottomLabel.sizeToFit()
            bottomLabel.numberOfLines = 0
            bottomLabel.snp.makeConstraints { (make) in
                make.top.equalTo(cell.contentView.snp.bottom).offset(-60)
                make.width.equalTo(kScreenWidth - 50)
                make.centerX.equalTo(cell.contentView)
            }
            
            bottomLabel.textColor = UIColor("#2E82FF")
            bottomLabel.textAlignment = .center
            bottomLabel.adjustsFontSizeToFitWidth = true
            bottomLabel.font = UIFont.systemFont(ofSize: 14)
            if myDataModel.total_releasing != nil{
                 bottomLabel.text = String(format: "%@ %@", XALanguageManager.getString(key: "The team has accumulated a total of"),myDataModel.total_releasing!) 
            }
           
            
        }
        return cell
    }
    //footer height
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    // row height
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 230
        }
        return 70
    }
    // header height
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    // row num
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
        
    }
    // 组数
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    //点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
    }
    
    
    // MARK: - 懒加载
    private lazy var myTableView:UITableView = {
        let myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: UITableViewStyle.grouped)
        myTableView.delegate = self
        myTableView.dataSource = self
        myTableView.separatorStyle = .none; myTableView.register(MyTeamTableViewCell.self, forCellReuseIdentifier: "cell")
        myTableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
        return myTableView
    }()
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
